package com.abe.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.abe.config.Constants;
import com.abe.config.SettingParam;
import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.threadmill.R;
import com.abe.utils.JsonHelper;
import com.abe.utils.NormalUtils;
import com.abe.utils.SPUtils;

@SuppressWarnings("deprecation")
public class GuideActivity extends BaseActivity {

	private View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		/** 国际化设置 */
		NormalUtils.setAppLanguage();
		/** 游客信息 */
		String tourist_json = JsonHelper.toJson(SettingParam.getTouristUser());
		SPUtils.put(getApplicationContext(), SettingParam.SP_CurrentUser,
				tourist_json);
		/** 初始化APP相关文件夹 */
		Constants.initAPPFolder();
		super.onCreate(savedInstanceState);
		view = View.inflate(this, R.layout.activity_guide, null);
		setContentView(view);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		into();
	}

	private void initView() {
		RelativeLayout guide_image = (RelativeLayout) findViewById(R.id.guide_image);
		guide_image.setBackgroundDrawable(NormalUtils.readDrawable(this,
				R.drawable.app_guide));
	}

	private void into() {
		Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.anim_guide);
		view.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				gotoActivityForResult(LoginActivity.class, 1);
				// gotoActivityForResult(TestActivity.class, 1);
			}
		});
	}

	/** 回调方法，从预览页面回来的时候会执行这个方法 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			int extra = data.getIntExtra("extra", Constants.ExitApp);
			if (extra == Constants.SettingLanguage) {
				/** 更新语言后，destroy当前页面，重新绘制 */
				finish();
				Intent it = new Intent(GuideActivity.this, GuideActivity.class);
				it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(it);
			} else {
				onBackPressed();
			}
		} else {
			onBackPressed();
		}
	}

}
